package cn.com.surker.material.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.enums.system.DataBaseEnum;
import cn.com.surker.material.dao.MainMaterialContractPriceDao;
import cn.com.surker.material.dao.MainMaterialTransportCarDao;
import cn.com.surker.material.entity.MainMaterialContractPrice;
import cn.com.surker.material.entity.MainMaterialTransportCar;
import cn.com.surker.material.entity.MainMaterialWeight;
import cn.com.surker.material.service.MaterialPriceBaseService;
import cn.com.surker.oa.dao.OaMaterialContractDao;
import cn.com.surker.oa.dao.OaMaterialDao;
import cn.com.surker.oa.dao.OaSupplierDao;
import cn.com.surker.oa.entity.OaMaterialContract;
import cn.com.surker.oa.entity.OaSupplier;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.dao.DataSourceInfoDao;
import cn.com.surker.system.dao.SysOriginDao;
import cn.com.surker.system.entity.DataSourceInfo;
import cn.com.surker.system.entity.SysOrigin;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.main.MainMaterialPriceVo;
import cn.com.surker.weight.dao.WeightTareDao;
import cn.com.surker.weight.entity.WeightTare;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author lucky
 * @create 2024年09月04 9:06
 * @desc
 */
public abstract class MaterialPriceBaseServiceImpl implements MaterialPriceBaseService {

    @Resource
    protected MainMaterialContractPriceDao materialContractPriceDao;
    @Resource
    protected MainMaterialTransportCarDao materialTransportCarDao;
    @Resource
    protected OaMaterialContractDao materialContractDao;
    @Resource
    protected OaSupplierDao supplierDao;
    @Resource
    protected OaMaterialDao materialDao;
    @Resource
    protected SysOriginDao originDao;
    @Resource
    protected WeightTareDao weightTareDao;
    @Resource
    protected DataSourceInfoDao dsDao;

    /**
     * 分页查询价格
     * @param searchVal
     * @param contractId
     * @param offset
     * @param limit
     * @return
     */
    public IPage findPage(String searchVal, Long contractId, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<MainMaterialContractPrice> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        if (!StringUtils.isNull(searchVal)) {
            query.and(orderWrapper -> orderWrapper.like("remark", searchVal).or()
                    .inSql("id", "select id from main_material_contract_price where status = 0 and contract_id = "
                            + contractId + " and material_id in (\n" +
                            "select id from oa_material where status = 0 and full_name like '%" + searchVal + "%')"));
        }
        query.orderByDesc("take_time");
        IPage<MainMaterialContractPrice> iPage = materialContractPriceDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 查询生产合同运输单价信息
     * @param contractId
     * @return
     */
    public Map<String, List<MainMaterialContractPrice>> findByContract(Long contractId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<MainMaterialContractPrice> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        query.orderByDesc("take_time");
        List<MainMaterialContractPrice> priceList = materialContractPriceDao.selectList(query);
        if (priceList == null || priceList.size() == 0)
            return null;
        Map<String, List<MainMaterialContractPrice>> map = new LinkedHashMap<>();
        for (int i = 0; i < priceList.size(); i++) {
            MainMaterialContractPrice price = priceList.get(i);
            String takeTime = DateUtil.convertString(price.getTakeTime());
            if (map.containsKey(takeTime)) {
                List<MainMaterialContractPrice> tempList = map.get(takeTime);
                tempList.add(price);
                map.put(takeTime, tempList);
            } else {
                List<MainMaterialContractPrice> tempList = new ArrayList<>();
                tempList.add(price);
                map.put(takeTime, tempList);
            }
        }
        return map;
    }
    /**
     * 根据合同查询材料单价信息
     * @param contractId
     * @param type
     * @return
     */
    public List<MainMaterialPriceVo> findByContract(Long contractId, Integer type) {
        if (StringUtils.isAllParamNull(contractId, type))
            return null;
        List<MainMaterialPriceVo> list = materialContractPriceDao.findByContract(contractId, type);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }
    public List<MainMaterialPriceVo> findByContractV1(Long contractId, Integer type) {
        if (StringUtils.isAllParamNull(contractId, type))
            return null;
        List<MainMaterialPriceVo> list = materialContractPriceDao.findByContractV1(contractId, type);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 查询供应商下一个单价信息
     * @return
     */
    public MainMaterialContractPrice findNextPrice(Long orgId, Long supplierId, Long transportId, Long materialId,
                                                   Integer type, String startTime, String endTime) {
        MainMaterialContractPrice contractPrice = materialContractPriceDao.findTakePrice(orgId, supplierId, transportId, materialId, type, startTime, endTime);
        return contractPrice;
    }
    /**
     * 保存合同价格
     * @param contractPrice
     * @return
     */
    public RestResultDto save(MainMaterialContractPrice contractPrice) {
        if (StringUtils.isNull(contractPrice.getMaterialId()))
            throw new MessageCenterException("材料为空");
        if (StringUtils.isNull(contractPrice.getOrgId()))
            throw new MessageCenterException("公司信息为空");
        if (StringUtils.isNull(contractPrice.getSupplierId()))
            throw new MessageCenterException("供应商信息为空");

        Long supplierId = null;
        Long transportId = null;
        Long materialId = null;
        if (!StringUtils.isNull(contractPrice.getSupplierId()))
            supplierId = contractPrice.getSupplierId();
        if (!StringUtils.isNull(contractPrice.getTransportId()))
            transportId = contractPrice.getTransportId();
        if (!StringUtils.isNull(contractPrice.getMaterialId()))
            materialId = contractPrice.getMaterialId();
        MainMaterialContractPrice temp = materialContractPriceDao.queryContractOrSupplier(supplierId,
                transportId, contractPrice.getContractId(),materialId,contractPrice.getType(), null);
        if (temp != null && contractPrice.getTakeTime() != null) {
            String priceTakeTime = DateUtil.convertString(contractPrice.getTakeTime());
            String tempTakeTime = DateUtil.convertString(temp.getTakeTime());
            if (priceTakeTime.equals(tempTakeTime))
                throw new MessageCenterException("同一生效时间存在相同价格信息");
            String remark = priceTakeTime + "起";
            if (contractPrice.getTaxPrice() > temp.getTaxPrice())
                remark += "含税上调" + (contractPrice.getTaxPrice() - temp.getTaxPrice()) + "元\t";
            else if (contractPrice.getTaxPrice() < temp.getTaxPrice())
                remark += "含税下调" + (temp.getTaxPrice() - contractPrice.getTaxPrice()) + "元\t";
            if (contractPrice.getPrice() > temp.getPrice())
                remark += "不含税上调" + (contractPrice.getPrice() - temp.getPrice()) + "元";
            else if (contractPrice.getPrice() < temp.getPrice())
                remark += "不含税下调" + (temp.getPrice() - contractPrice.getPrice()) + "元";
            if (StringUtils.isNull(contractPrice.getPriceRemark()))
                contractPrice.setPriceRemark(remark);
        }
        Date nowDate = new Date();
        contractPrice.setCreateTime(nowDate);
        Integer code = materialContractPriceDao.insert(contractPrice);
        if (code > 0)
            return RestResultDto.success("新增成功~！",contractPrice);

        return RestResultDto.fail("新增失败~!");
    }

    /**
     * 批量新增
     * @param list
     * @return
     */
    public RestResultDto saveBatch(List<MainMaterialContractPrice> list, String creator) {
        if (list == null || list.size() == 0)
            throw new MessageCenterException("参数错误");
        Date nowDate = new Date();

        List<MainMaterialContractPrice> tempList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MainMaterialContractPrice price = list.get(i);
            if (StringUtils.isAllParamNull(price.getContractId(), price.getMaterialId(),
                    price.getSupplierId(), price.getType(), price.getTakeTime()))
                continue;
            Long supplierId = null;
            Long transportId = null;
            Long materialId = null;
            if (!StringUtils.isNull(price.getSupplierId()))
                supplierId = price.getSupplierId();
            if (!StringUtils.isNull(price.getTransportId()))
                transportId = price.getTransportId();
            if (!StringUtils.isNull(price.getMaterialId()))
                materialId = price.getMaterialId();
            MainMaterialContractPrice temp = materialContractPriceDao.queryContractOrSupplier(supplierId,
                    transportId, price.getContractId(),materialId,price.getType(), null);
            if (temp != null && price.getTakeTime() != null) {
                String priceTakeTime = DateUtil.convertString(price.getTakeTime());
                String tempTakeTime = DateUtil.convertString(temp.getTakeTime());
                if (priceTakeTime.equals(tempTakeTime))
                    throw new MessageCenterException("同一生效时间存在相同价格信息");
                String remark = priceTakeTime + "起";
                if (price.getTaxPrice() > temp.getTaxPrice())
                    remark += "含税上调" + (price.getTaxPrice() - temp.getTaxPrice()) + "元\t";
                else if (price.getTaxPrice() < temp.getTaxPrice())
                    remark += "含税下调" + (temp.getTaxPrice() - price.getTaxPrice()) + "元\t";
                if (price.getPrice() > temp.getPrice())
                    remark += "不含税上调" + (price.getPrice() - temp.getPrice()) + "元";
                else if (price.getPrice() < temp.getPrice())
                    remark += "不含税下调" + (temp.getPrice() - price.getPrice()) + "元";
                if (StringUtils.isNull(price.getPriceRemark()))
                    price.setPriceRemark(remark);
            }
            price.setCreateTime(nowDate);
            price.setCreator(creator);
            tempList.add(price);
        }
        if (tempList == null || tempList.size() == 0)
            return RestResultDto.fail("没有任何新增数据~！");
        Integer code = materialContractPriceDao.saveBatch(tempList);
        if (code > 0)
            return RestResultDto.success("新增成功~！",tempList);

        return RestResultDto.fail("新增失败~!");
    }

    /**
     * 更新合同价格
     * @param contractPrice
     * @return
     */
    public RestResultDto updateContractPrice(MainMaterialContractPrice contractPrice) {
        if (contractPrice == null || StringUtils.isNull(contractPrice.getId()))
            return RestResultDto.fail("参数错误");
        MainMaterialContractPrice tempContract = materialContractPriceDao.selectById(contractPrice.getId());
        if (tempContract == null || tempContract.getStatus().intValue() == 1)
            return RestResultDto.fail("修改实体不存在");
        if (!StringUtils.isNull(contractPrice.getPrice()) && contractPrice.getPrice() != tempContract.getPrice())
            tempContract.setPrice(contractPrice.getPrice());
        if (!StringUtils.isNull(contractPrice.getTaxPrice()) && contractPrice.getTaxPrice() != tempContract.getTaxPrice())
            tempContract.setTaxPrice(contractPrice.getTaxPrice());
        if (!StringUtils.isNull(contractPrice.getType()) && contractPrice.getType() != tempContract.getType())
            tempContract.setType(contractPrice.getType());
        if (!StringUtils.isNull(contractPrice.getIsBill()) && contractPrice.getIsBill() != tempContract.getIsBill())
            tempContract.setIsBill(contractPrice.getIsBill());
        if (!StringUtils.isNull(contractPrice.getRemark()) && !contractPrice.getRemark().equals(tempContract.getRemark()))
            tempContract.setRemark(contractPrice.getRemark());
        if (!StringUtils.isNull(contractPrice.getPriceRemark()) && !contractPrice.getPriceRemark().equals(tempContract.getPriceRemark()))
            tempContract.setPriceRemark(contractPrice.getPriceRemark());
        if (!StringUtils.isNull(contractPrice.getTakeTime()) && !StringUtils.isNull(tempContract.getTakeTime())) {
            String oldTakeTime = DateUtil.convertString(tempContract.getTakeTime());
            String newTakeTime = DateUtil.convertString(contractPrice.getTakeTime());
            if (!oldTakeTime.equals(newTakeTime))
                tempContract.setTakeTime(contractPrice.getTakeTime());
        }
        tempContract.setUpdateTime(new Date());
        if (!StringUtils.isNull(contractPrice.getUpdator()))
            tempContract.setUpdator(contractPrice.getUpdator());
        Integer code = materialContractPriceDao.updateById(tempContract);
        if (code > 0)
            return RestResultDto.success("修改成功~！", tempContract);
        return RestResultDto.fail("修改失败~！");
    }

    /**
     * 禁用合同价格
     * @param id
     * @param status
     * @return
     */
    public RestResultDto updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            return RestResultDto.fail("参数错误");

        MainMaterialContractPrice tempContract = materialContractPriceDao.selectById(id);
        if (tempContract == null)
            return RestResultDto.fail("修改实体不存在");
        tempContract.setUpdator(creator);
        tempContract.setStatus(status);
        tempContract.setUpdateTime(new Date());
        Integer code = materialContractPriceDao.updateById(tempContract);
        if (code > 0)
            return RestResultDto.success("修改成功~！");
        return RestResultDto.fail("修改失败~！");
    }

    /**
     * 新增运输合同车辆
     * @param contractId
     * @param carNo
     * @param remark
     * @param creator
     * @return
     */
    public RestResultDto saveContractCar(Long contractId, String carNo, String remark, String creator, String today,
                                         String startTime, String endTime, Long supplierId) {
        if (StringUtils.isAllParamNull(contractId, carNo, creator))
            return RestResultDto.fail("参数错误");
        carNo = carNo.replaceAll(" ", ",").replaceAll("\n", "");
        carNo = carNo.toUpperCase();
        Date nowDate = new Date();
        Date takeDate = nowDate;
        if (StringUtils.isNull(today))
            throw new MessageCenterException("请选择统计周期");
        if (StringUtils.isNull(startTime))
            throw new MessageCenterException("请选择车辆服务开始时间");
        if (StringUtils.isNull(endTime))
            throw new MessageCenterException("请选择车辆服务结束时间");
        if (StringUtils.isNull(supplierId))
            throw new MessageCenterException("请选择托运商");
        if (!StringUtils.validatorCarNo(carNo))
            return RestResultDto.fail("车牌号格式错误");
        Integer total = materialTransportCarDao.findExistCarNo(contractId,carNo,today,startTime,supplierId);
        if (total > 0)
            return RestResultDto.fail("车牌号已存在");
        Double tare = 0.0;
        WeightTare weightTare = weightTareDao.findByCarNo(carNo);
        if(weightTare != null)
            tare = weightTare.getTare();
        MainMaterialTransportCar transportCar = new MainMaterialTransportCar(contractId, carNo, today, startTime,
                endTime, tare, 0, remark, creator, nowDate, supplierId);
        Integer code = materialTransportCarDao.insert(transportCar);
        if (code > 0)
            return RestResultDto.success("新增成功~！",transportCar);
        return RestResultDto.fail("新增失败~！");
    }
    /**
     * 新增运输合同车辆
     * @param contractId
     * @param carNos
     * @param remark
     * @param creator
     * @return
     */
    public RestResultDto saveBatchContractCar(Long contractId, String carNos, String remark, String creator,
                                              String today, String startTime, String endTime, Long supplierId) {
        if (StringUtils.isAllParamNull(contractId, carNos, creator))
            return RestResultDto.fail("参数错误");
        carNos = carNos.replaceAll(" ", ",").replaceAll("\n", "");
        String[] carNos_ = carNos.split(",");
        Date nowDate = new Date();
        Date takeDate = nowDate;
        if (carNos_ == null || carNos_.length == 0)
            throw new MessageCenterException("没有需要新增的车辆");
        if (StringUtils.isNull(today))
            throw new MessageCenterException("请选择统计周期");
        if (StringUtils.isNull(startTime))
            throw new MessageCenterException("请选择车辆服务开始时间");
        if (StringUtils.isNull(endTime))
            throw new MessageCenterException("请选择车辆服务结束时间");
        if (StringUtils.isNull(supplierId))
            throw new MessageCenterException("请选择托运商");
        QueryWrapper<WeightTare> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.in("bus_number", carNos_);
        String tips = "";
        List<WeightTare> weightTareList = weightTareDao.selectList(query);
        List<MainMaterialTransportCar> carList = new ArrayList<>();
        Map<String, Double> carMap = new HashMap<>();
        if (carList != null && weightTareList.size() > 0) {
            for (int i = 0; i < weightTareList.size(); i++) {
                WeightTare transportCar = weightTareList.get(i);
                carMap.put(transportCar.getBusNumber(), transportCar.getTare());
            }
        }
        for (int i = 0; i < carNos_.length; i++) {
            String carNo = carNos_[i].replaceAll(" ", "");
            carNo = carNo.toUpperCase();
            if (StringUtils.isNull(carNo))
                continue;
            if (!StringUtils.validatorCarNo(carNo)) {
                tips += carNo + "车牌号格式错误。";
                continue;
            }
            Integer total = materialTransportCarDao.findExistCarNo(contractId,carNo,today,startTime,supplierId);
            if (total > 0) {
                tips += carNo + "车牌号已存在。";
                continue;
            }
            Double tare = 0.0;
            if (carMap.containsKey(carNo))
                tare = carMap.get(carNo);
            MainMaterialTransportCar transportCar = new MainMaterialTransportCar(contractId, carNo, today, startTime,
                    endTime, tare, 0, remark, creator, nowDate, supplierId);
            carList.add(transportCar);
        }
        Integer code = materialTransportCarDao.saveBatch(carList);
        if (code > 0) {
            String message = "批量新增车辆成功";
            if (!StringUtils.isNull(tips))
                message = tips;
            return RestResultDto.success(message);
        }
        return RestResultDto.fail("批量新增车辆错误");
    }

    /***
     * 批量删除绑定车辆
     * @param ids
     * @param creator
     * @return
     */
    public RestResultDto deleteBatchContractCar(String ids, String creator) {
        if (StringUtils.isNull(ids))
            throw new MessageCenterException("参数错误");
        String[] ids_ = ids.split(",");
        String nowTime = DateUtil.convertString(new Date());
        UpdateWrapper<MainMaterialTransportCar> uw = new UpdateWrapper<>();
        uw.set("status", 1);
        uw.set("updator", creator);
        uw.set("update_time", nowTime);
        uw.in("id", ids_);
        materialTransportCarDao.update(new MainMaterialTransportCar(), uw);
        return RestResultDto.success("更新成功");
    }

    /**
     * 禁用合同车牌
     * @param id
     * @param creator
     * @return
     */
    public RestResultDto updateContractCarStatus(Long id, String creator) {
        if (StringUtils.isAllParamNull(id))
            return RestResultDto.fail("参数错误");
        MainMaterialTransportCar transportCar = materialTransportCarDao.selectById(id);
        if (transportCar == null)
            return RestResultDto.fail("修改实体不存在");
        transportCar.setStatus(1);
        transportCar.setUpdateTime(new Date());
        transportCar.setUpdator(creator);
        Integer code = materialTransportCarDao.updateById(transportCar);
        if (code > 0)
            return RestResultDto.success("禁用成功~！");
        return RestResultDto.fail("禁用失败~！");
    }

    /**
     * 查询运输合同车辆
     * @param contractId
     * @return
     */
    public List<MainMaterialTransportCar> findByContractCar(Long contractId, String today, Long supplierId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<MainMaterialTransportCar> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        if (!StringUtils.isNull(today))
            query.eq("today", today);
        if (!StringUtils.isNull(supplierId))
            query.eq("supplier_id", supplierId);
        query.orderByDesc("create_time");
        List<MainMaterialTransportCar> list = materialTransportCarDao.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }
    /**
     * 查询某个合同下运输的车牌信息
     * @param orgId
     * @param supplierId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<MainMaterialTransportCar> findTodayWeightCar(Long orgId, Long contractId, Long supplierId, String startTime, String endTime) {
        if (StringUtils.isAllParamNull(orgId,contractId))
            throw new MessageCenterException("获取过磅车牌信息参数错误");
        DataSourceInfo ds = dsDao.queryDSByOriginIdV1(orgId, DataBaseEnum.MYSQL.getCode());
        if (ds == null || ds.getType() != 0)
            throw new MessageCenterException("该功能只支持新系统");
        List<MainMaterialTransportCar> list = materialTransportCarDao.findTodayWeightCar(ds.getCode(), orgId, contractId,
                supplierId, startTime, endTime);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 复制车牌号
     * @return
     */
    public RestResultDto copyCarNo(Long contractId, Long copySupplierId, String copyToday, String today,
                                   String startTime, String endTime, Long newSupplierId, String remark, String creator) {
        if (StringUtils.isAllParamNull(contractId, copyToday, today, startTime, endTime))
            throw new MessageCenterException("参数错误");
        QueryWrapper<MainMaterialTransportCar> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        query.eq("today", copyToday);
        if (!StringUtils.isNull(copySupplierId))
            query.eq("supplier_id", copySupplierId);
        query.orderByDesc("create_time");
        List<MainMaterialTransportCar> list = materialTransportCarDao.selectList(query);
        if (list == null || list.size() == 0)
            throw new MessageCenterException("没有获取到复制车牌列表");
        Date nowDate = new Date();
        List<MainMaterialTransportCar> tempList = new ArrayList<>();
        String tips = "";
        for (int i = 0; i < list.size(); i++) {
            MainMaterialTransportCar tempCar = list.get(i);
            Long tempSupplierId = tempCar.getSupplierId();
            if (!StringUtils.isNull(newSupplierId))
                tempSupplierId = newSupplierId;
            tempCar.setId(null);
            Integer total = materialTransportCarDao.findExistCarNo(contractId, tempCar.getCarNo(),today,startTime,tempSupplierId);
            if (total > 0) {
                tips += tempCar.getCarNo() + "车牌号已存在。";
                continue;
            }
            String remark_ = StringUtils.convertString(remark) + " " + creator + "复制" + copyToday + "的车牌新增至" + today;
            tempCar.setToday(today);
            tempCar.setRemark(remark_);
            tempCar.setStartTime(startTime);
            tempCar.setEndTime(endTime);
            tempCar.setCreator(creator);
            tempCar.setCreateTime(nowDate);
            tempCar.setSupplierId(tempSupplierId);
            tempList.add(tempCar);
        }
        if (tempList == null || tempList.size() == 0)
            throw new MessageCenterException("没有任何新增数据");
        Integer code = materialTransportCarDao.saveBatch(tempList);
        if (code > 0) {
            String message = "复制新增车辆成功";
            if (!StringUtils.isNull(tips))
                message = tips;
            return RestResultDto.success(message);
        }
        throw new MessageCenterException("复制新增车牌信息异常");
    }

    /**
     * 材料运输车辆分页
     * @param searchVal
     * @param contractId
     * @param offset
     * @param limit
     * @return
     */
    public IPage findContractCarPage(String searchVal, Long contractId, String today, Long supplierId, Integer offset, Integer limit) {
        if (StringUtils.isNull(offset))
            offset = 0;
        if (StringUtils.isNull(limit))
            limit = 15;
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<MainMaterialTransportCar> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        if (!StringUtils.isNull(today))
            query.eq("today", today);
        if (!StringUtils.isNull(supplierId))
            query.eq("supplier_id", supplierId);
        if (!StringUtils.isNull(searchVal))
            query.and(orderWrapper -> orderWrapper.like("car_no", searchVal).or()
                    .like("remark", searchVal));
        IPage<MainMaterialTransportCar> iPage = materialTransportCarDao.selectPage(new Page<>((offset + limit) / limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 更新车辆信息
     * @param transportCar
     * @return
     */
    public RestResultDto updateContractCar(MainMaterialTransportCar transportCar) {
        if (transportCar == null || StringUtils.isNull(transportCar.getId()))
            return RestResultDto.fail("修改实体不存在");

        if (!StringUtils.isNull(transportCar.getCarNo())) {
            transportCar.setCarNo(transportCar.getCarNo().toUpperCase());
            if (!StringUtils.validatorCarNo(transportCar.getCarNo()))
                return RestResultDto.fail("车牌号格式错误");
        }

        transportCar.setUpdateTime(new Date());
        Integer code = materialTransportCarDao.updateById(transportCar);
        if (code > 0)
            return RestResultDto.success("修改成功~！");
        return RestResultDto.fail("修改失败~！");
    }
}
